package com.erp.ae.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.ae.service.IAeStoreOrderReceiptAddressService;
import com.erp.ae.service.IAeStoreOrderService;
import com.erp.ae.service.IAeWarehouseOrderInfoService;
import com.global.iop.domain.Protocol;
import com.ruoyi.ae.api.domain.AeStoreOrderReceiptAddress;
import com.ruoyi.ae.api.domain.AeWarehouseOrderInfo;
import com.ruoyi.common.core.domain.R;
import com.erp.ae.utils.AeApiUtils;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/erpOrder")
public class AeOrderController {
    private final IAeStoreOrderService iAeStoreOrderService;
    private final IAeWarehouseOrderInfoService warehouseOrderInfoService;

    /**
     * 获取线上物流面单
     */
    @GetMapping("/getOnlineLogisticsLabel/{orderId}")
    public AeWarehouseOrderInfo getOnlineLogisticsLabel(@PathVariable("orderId") Long orderId) {
        return warehouseOrderInfoService.getOnlineLogisticsLabel(orderId);
    }

    @GetMapping("/findIssueOrderListByStoreId/{storeId}")
    public R<Object> findIssueOrderListByStoreId(@PathVariable("storeId") String storeId){
        //生成参数Map
        //获取店铺的accessToken
        String accessToken = AeApiUtils.getToken(storeId);
//        Long childOrderId = (Long) map.get("childOrderId");
//        Long orderId = (Long) map.get("orderId");

//        Map<String, Object>  paremeterMap = new HashMap<>();

        String msg =
                AeApiUtils.sendRequest(
                        "aliexpress.issue.issuelist.get",
                        "query_dto",
                        "{\"current_page\":\"1\",\"page_size\":\"50\"}",
                        Protocol.TOP,
                        accessToken
                );
        System.out.println(msg);

        if (!msg.contains("data_list")) {
            return R.fail("未查找到订单纠纷信息");
        }

        String dataList = JSONObject
                .parseObject(msg)
                .getJSONObject("aliexpress_issue_issuelist_get_response")
                .getJSONObject("data_list")
                .getString("issue_api_issue_dto");
        JSONArray jsonArray = JSONArray.parseArray(dataList);
//        for (int i = 0; i < jsonArray.size(); i++) {
//            JSONObject jsonObject = jsonArray.getJSONObject(i);
//            String issueId = jsonObject.getString("issue_id");
//            String buyerLoginId = jsonObject.getString("buyer_login_id");
//            Map<String, Object> issueMap = new HashMap<>();
//            issueMap.put("buyer_login_id", buyerLoginId);
//            issueMap.put("issue_id", issueId);
//            String issueMsg =
//                    AeApiUtils.sendRequest(
//                            "aliexpress.issue.detail.get",
//                            issueMap,
//                            Protocol.TOP,
//                            accessToken
//                    );
//            System.out.println(issueMsg);
//        }

//        return R.ok(jsonArray.size());
        return R.ok(dataList);
    }


    /**
     * 订单基础信息查询
     */
    @PostMapping("/findOrderBaseInfo")
    public R<Object> findOrderBaseInfo(@RequestBody Map<String, String> map) throws Exception {
        String storeId = (String) map.get("storeId");
        String accessToken = AeApiUtils.getToken(storeId);
        String orderId = (String) map.get("orderId");
       return iAeStoreOrderService.findOrderBaseInfo(accessToken,orderId);
    }

    /**
     * 获取订单收货信息
     */
    @PostMapping("/findOrderReceiptInfo")
    public R<Object> findOrderReceiptInfo(@RequestBody Map<String, Object> map) throws Exception {
        String storeId = (String) map.get("storeId");
        String accessToken = AeApiUtils.getToken(storeId);
        Long orderId = (Long) map.get("orderId");
        return iAeStoreOrderService.findOrderReceiptInfo(accessToken,orderId);
    }

    /**
     * 获取订单收货地址信息
     *
     * @param orderId
     */
    @GetMapping("/findOrderReceiptInfo/{storeId}/{orderId}")
    public R<Object> findOrderReceiptInfo(@PathVariable("storeId") String storeId, @PathVariable("orderId") String orderId) {
        String accessToken = AeApiUtils.getToken(storeId);
        return iAeStoreOrderService.findOrderReceiptInfo(accessToken,Long.parseLong(orderId));
    }

    /**
     * 获取订单佣金和币种
     */
    @PostMapping("/findOrderFeeRateAndCodeInfo")
    public R<Object> findOrderFeeRateAndCodeInfo(@RequestBody Map<String, Object> map) throws Exception {
        String storeId = (String) map.get("storeId");
        String accessToken = AeApiUtils.getToken(storeId);
        Long orderId = (Long) map.get("orderId");
        return iAeStoreOrderService.findOrderFeeRateAndCodeInfo(accessToken,orderId);
    }

    /**
     * 获取订单金额（new_order_amount和logistics_amount）
     */
    @PostMapping("updateOrderAmount")
    public R<Object> updateOrderAmount(@RequestBody Map<String, Object> sendMap) {
        String storeId = (String) sendMap.get("storeId");
        String accessToken = AeApiUtils.getToken(storeId);
        Long orderId = (Long) sendMap.get("orderId");
      return iAeStoreOrderService.updateOrderAmount(accessToken,orderId);

    }




}
